//
//  WBTabBarController.swift
//  WBTest
//
//  Created by pengZY on 2017/9/25.
//  Copyright © 2017年 zhang. All rights reserved.
//

import UIKit

class WBTabBarController: UITabBarController {
      private lazy var plusBtn : UIButton = UIButton.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        let sources =  WBTabBarSources().sources
        var arrM = [UIViewController]()
        for dict in sources {
          arrM.append(getChirdController(dic: dict))
        }
        self.viewControllers = arrM
     
        tabBar.addSubview(plusBtn)
    plusBtn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: .normal)
    plusBtn.setBackgroundImage(UIImage.init(named:"tabbar_compose_button"), for: .normal)
    plusBtn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: .selected)
 plusBtn.setBackgroundImage(UIImage.init(named:"tabbar_compose_button_highlighted"), for: .selected)
        let width = tabBar.bounds.width / CGFloat((self.viewControllers?.count)!)
        plusBtn.frame = tabBar.bounds.insetBy(dx: 2*width, dy: 0)
        plusBtn.addTarget(self, action: #selector(didClickPlusBtn), for: .touchUpInside)
        
    }
    
    @objc  private func didClickPlusBtn(){
        print("写微博")
    }


}
extension WBTabBarController{
    
   
    //获取 nav控制器
   func getChirdController(dic:[String:String]) -> UIViewController {
        
        guard let title = dic["title"],let imageName = dic["imageName"],let name = dic["clsName"],let selectedName = dic["selectedName"] , let cs = NSClassFromString(Bundle.main.spaceName+"."+name) as? UIViewController.Type  else {
            return UIViewController()
        }
        
        let vc = cs.init()
    
        let nav = WBNavigationController.init(rootViewController: vc)
        nav.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        nav.title = title
    nav.tabBarItem.image = UIImage.init(named: imageName)
    nav.tabBarItem.selectedImage = UIImage.init(named: selectedName)
        return nav
    }
}
